<script>
	import jsonData from '@/static/init.json';

	export default {
		onLaunch: function() {
			console.log('App Launch')

		},
		onShow: function() {
			console.log('App Show')
			this.initData();
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {

			showToast(msg) {
				uni.showToast({
					title: msg,
					icon: 'none'
				})
			},
			initData() {


				// 获取当前页面的完整URL
				const currentUrl = window.location.href;

				// 找到index.html在URL中的位置
				const indexHtmlIndex = currentUrl.indexOf('index.html');

				// 提取index.html之前的部分作为index.html的URL
				const indexHtmlUrl = currentUrl.substring(0, indexHtmlIndex + 10); // 10 是 'index.html' 的长度

				console.log(indexHtmlUrl);

				var host = indexHtmlUrl.replaceAll("index.html", "");

				var that = this;
				uni.request({
					url: host + '/static/init.json',
					success: function(res) {
						that.checkAppVersion(res.data);
					},
					fail: function(err) {
						console.error(err);
					}
				});
			},
			checkAppVersion(res) {

				let netVersion = res.netVersion;
				let appVersion = "1.0";

				try {
					appVersion = AndroidInterface.getAppVersionName();
				} catch (e) {
					//TODO handle the exception
				}

				var net = Number(netVersion);
				var app = Number(appVersion);

				if (net > app) {
					uni.reLaunch({
						url: "/pages/upgrade/upgrade"
					});
					uni.setNavigationBarTitle({
						title: '软件更新'
					});

				} else {
					uni.reLaunch({
						url: "pages/ad/ad"
					});
					uni.setNavigationBarTitle({
						title: '热门推广'
					});

				}
			}

		}
	}
</script>

<style>
	/*每个页面公共css */
	body {
		background-color: rgba(0, 0, 0, 0.3);
		color: white;
	}
</style>